package com.smzd.ccms.actions;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smzd.ccms.business.CompletConfigService;
import com.smzd.ccms.business.CompletParamConfigService;
import com.smzd.ccms.domain.CompletConfig;
import com.smzd.ccms.domain.CompletParamConfig;
import com.smzd.framework.annotation.Access;
import com.smzd.framework.core.actions.AbstractScopedController;
import com.smzd.framework.core.business.IFGService;
import com.smzd.framework.utils.db.SRPage;

@Controller
@RequestMapping("/completcfg/{completcfgId}/cpconfigs")
@Access("TAG_ROLE_CCMS")
public class CompletParamConfigController extends AbstractScopedController<CompletParamConfig, CompletConfig> {
	@Autowired
	private CompletParamConfigService service;

	@Override
	protected IFGService<CompletParamConfig> getService() {
		return service;
	}

	@Autowired
	private CompletConfigService completcfgService;

	@Override
	protected CompletConfig getScope(int scopeId) {
		return completcfgService.getItemById(scopeId);
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	@Override
	public String toAddPage(Map<String, Object> model, @PathVariable("completcfgId") int scopeId,
	        CompletParamConfig elem, BindingResult result) throws Exception {
		return super.toAddPage(model, scopeId, elem, result);
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public String postAddData(Map<String, Object> model, @PathVariable("completcfgId") int scopeId,
	        CompletParamConfig elem, BindingResult result) throws Exception {
		return super.postAddData(model, scopeId, elem, result);
	}

	@RequestMapping(method = RequestMethod.GET)
	@Override
	public String toListPage(Map<String, Object> model, @PathVariable("completcfgId") int scopeId,
	        CompletParamConfig elem, BindingResult result) throws Exception {
		return super.toListPage(model, scopeId, elem, result);
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public String getListData(Map<String, Object> model, @PathVariable("completcfgId") int scopeId,
	        SRPage<CompletConfig, CompletParamConfig> page, BindingResult result) throws Exception {
		return super.getListData(model, scopeId, page, result);
	}

	@RequestMapping(value = "/all", method = RequestMethod.POST)
	@ResponseBody
	public String getAllParamsConfigInJsonString(@PathVariable("completcfgId") int scopeId) throws Exception {
		CompletConfig cconfig = getScope(scopeId);
		return service.getAllParamsConfigInJsonString(cconfig);
	}

	@RequestMapping(value = "/edit", method = RequestMethod.GET)
	@Override
	public String toEditPage(Map<String, Object> model, @PathVariable("completcfgId") int scopeId,
	        CompletParamConfig elem, BindingResult result) throws Exception {
		elem = service.getItemById(elem.getId());
		return super.toEditPage(model, scopeId, elem, result);
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public String postEditData(Map<String, Object> model, @PathVariable("completcfgId") int scopeId,
	        CompletParamConfig elem, BindingResult result) throws Exception {
		return super.postEditData(model, scopeId, elem, result);
	}

	@RequestMapping(value = "/remove", method = RequestMethod.POST)
	@ResponseBody
	@Override
	public String postRemoveData(Map<String, Object> model, @PathVariable("completcfgId") int scopeId,
	        CompletParamConfig elem, BindingResult result) throws Exception {
		return super.postRemoveData(model, scopeId, elem, result);
	}

	@RequestMapping(value = "/{paramId}/edit", method = RequestMethod.POST)
	@ResponseBody
	public String customizeParamValue(@PathVariable("completcfgId") int scopeId, @PathVariable("paramId") int paramId,
	        @RequestParam("value") String value, @RequestParam("alias") String alias, @RequestParam("source") int source)
	        throws Exception {
		CompletConfig cconfig = getScope(scopeId);
		return service.customizeParamValue(cconfig, paramId, value, alias, source);
	}

	@RequestMapping(value = "/{paramId}/restore", method = RequestMethod.POST)
	@ResponseBody
	public String restoreParamValue(@PathVariable("completcfgId") int scopeId, @PathVariable("paramId") int paramId)
	        throws Exception {
		CompletConfig cconfig = getScope(scopeId);
		return service.restoreParamValue(cconfig, paramId);
	}
}